
package org.soekrat.engine.io;

import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/*
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.SVNProperty;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
*/

public final class SvnUtil
{
  /**
   * <p>
   *   Data return 
   * </p>
   * @author Arne Halvorsen (AH)
   */
  public static final class SvnData
  {
    /**
     * <p>
     *   MIME type of data.
     * </p>
     */
    public final String mime;
    
    /**
     * <p>
     *   Stream of data.
     * </p>
     */
    public final InputStream stream;
    
    /**
     * <p>
     *   Constructor.
     * </p>
     * @param mime {@link #mime}.
     * @param is   {@link #stream}.
     */
    private SvnData(String mime, InputStream is)
    {
      this.mime=mime;
      this.stream=is;
    }
    
    @Override
    public String toString() 
    {
      try
      {
        CharArrayWriter caw=new CharArrayWriter();
        PrintWriter pw=new PrintWriter(caw);
        pw.println(this.mime);
      
        if (mime==null || mime.startsWith("text/"))
        {
          BufferedReader br=
            new BufferedReader(new InputStreamReader(this.stream));
          String line=br.readLine();
          while (line!=null)
          {
            pw.println(line);
            line=br.readLine();
          }
        }
      
        pw.flush();
        return caw.toString();
      }
      catch (IOException iox)
      {
        iox.printStackTrace();
        return "";
      }
    }
    
  }
  
  /*
  public static SvnData getData(String url, String name, String pass, 
    String path) throws IOException
  {
    if (url==null)
    {
      throw new NullPointerException("url");  
    }
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    if (pass==null)
    {
      throw new NullPointerException("pass");
    }
    
    DAVRepositoryFactory.setup();
    
    SVNRepository repository=null;
    
    try
    {
      repository=SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
      ISVNAuthenticationManager authManager=
        SVNWCUtil.createDefaultAuthenticationManager(name, pass);
      
      repository.setAuthenticationManager(authManager );
    
      SVNNodeKind nodeKind=repository.checkPath(path, -1);
                
      if (nodeKind==SVNNodeKind.NONE)
      {
        throw new IOException("there is no entry at '"+url+ "'.");
      } 
      else if ( nodeKind == SVNNodeKind.DIR )
      {
        throw new IOException("the entry at '"+url+
          "' is a directory while a file was expected.");
      }
      
      SVNProperties fileProperties=new SVNProperties();
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      
      repository.getFile(path, -1,fileProperties, baos);
      
      String mimeType=fileProperties.getStringValue(SVNProperty.MIME_TYPE);
      System.out.println("MIME: "+mimeType);
      return new SvnData(mimeType, 
        new ByteArrayInputStream(baos.toByteArray()));
    }
    catch (SVNException svnx)
    {
      throw new IOException(svnx);
    }
  }
  */
  
  public static void main( String[] args ) 
  {
    /*
    try
    {
      SvnData data=getData("http://soekrat.googlecode.com/svn", 
        "arne.halvorsen@gmail.com", "zx6gJ3rX7Qj9", 
        "/trunk/soekrat/soekrat/soekrat.xml");
      System.out.println(data);
    }
    catch (IOException iox)
    {
      iox.printStackTrace();
    }
    */
    /*
    DAVRepositoryFactory.setup( );
     
    String url = "http://soekrat.googlecode.com/svn";
    String name = "arne.halvorsen@gmail.com";
    String password = "zx6gJ3rX7Qj9";
    String filePath = "/trunk/soekrat/soekrat/soekrat.xml";
    
    SVNRepository repository = null;
    try 
    {
      repository=SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
      ISVNAuthenticationManager authManager=
        SVNWCUtil.createDefaultAuthenticationManager(name, password);
      
      repository.setAuthenticationManager(authManager );
    
      SVNNodeKind nodeKind = repository.checkPath(filePath, -1);
                
      if ( nodeKind == SVNNodeKind.NONE )
      {
        System.err.println( "There is no entry at '" + url + "'." );
        System.exit( 1 );
      } 
      else if ( nodeKind == SVNNodeKind.DIR )
      {
        System.err.println( "The entry at '" + url + 
          "' is a directory while a file was expected." );
        System.exit( 1 );
      }
      
      SVNProperties fileProperties=new SVNProperties();
      //Map fileProperties=new HashMap();
      ByteArrayOutputStream baos = new ByteArrayOutputStream( );
      repository.getFile( filePath , -1 , fileProperties , baos );
      
      String mimeType=fileProperties.getStringValue(SVNProperty.MIME_TYPE);
      //String mimeType = ( String ) fileProperties.get( SVNProperty.MIME_TYPE );
      boolean isTextType = SVNProperty.isTextMimeType( mimeType );
       
      
      Iterator iterator = fileProperties.keySet( ).iterator( );
      while ( iterator.hasNext( ) )
      {
        String propertyName = ( String ) iterator.next( );
        String propertyValue = ( String ) fileProperties.get( propertyName );
        System.out.println( "File property: " + propertyName + "=" + propertyValue );
      }
      
     
      if ( isTextType )
      {
        System.out.println( "File contents:" );
        System.out.println( );
        try 
        {
          baos.writeTo( System.out );
        }  
        catch ( IOException ioe ) 
        {
          ioe.printStackTrace( );
        }
      } 
      else 
      {
        System.out.println( "Not a text file." );
      } 
    }
    catch (Exception x)
    {
      x.printStackTrace();
    }
    */
  }
  
}
